/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "dal_rticonnextdds/listener_impl/SubscriberListenerImpl.hpp"
#include "dal_rticonnextdds/entity_impl/DalSubscriberImpl.hpp"
#include "dal_rticonnextdds/entity_impl/DalDataReaderImpl.hpp"
#include "dal_rticonnextdds/utils/log.hpp"
#include "dal_rticonnextdds/utils/EntityMappingManager.hpp"

SubscriberListenerImpl::SubscriberListenerImpl(dal_subscriber_listener_callbacks_t callbacks)
{
    this->callbacks_ = callbacks;
    this->rti_sub_listener_ = new DDS_SubscriberListener();
    *(this->rti_sub_listener_) = DDS_SubscriberListener_INITIALIZER;

    if (this->callbacks_.on_data_on_readers) {
        this->rti_sub_listener_->on_data_on_readers = SubscriberListenerImpl::on_data_on_readers;
    } else {
        this->rti_sub_listener_->on_data_on_readers =
            SubscriberListenerImpl::on_data_on_readers_dummy;
    }

    if (this->callbacks_.on_sample_lost) {
        this->rti_sub_listener_->as_datareaderlistener.on_sample_lost =
            SubscriberListenerImpl::on_sample_lost;
    } else {
        this->rti_sub_listener_->as_datareaderlistener.on_sample_lost =
            SubscriberListenerImpl::on_sample_lost_dummy;
    }

    if (this->callbacks_.on_requested_deadline_missed) {
        this->rti_sub_listener_->as_datareaderlistener.on_requested_deadline_missed =
            SubscriberListenerImpl::on_requested_deadline_missed;
    } else {
        this->rti_sub_listener_->as_datareaderlistener.on_requested_deadline_missed =
            SubscriberListenerImpl::on_requested_deadline_missed_dummy;
    }

    if (this->callbacks_.on_requested_incompatible_qos) {
        this->rti_sub_listener_->as_datareaderlistener.on_requested_incompatible_qos =
            SubscriberListenerImpl::on_requested_incompatible_qos;
    } else {
        this->rti_sub_listener_->as_datareaderlistener.on_requested_incompatible_qos =
            SubscriberListenerImpl::on_requested_incompatible_qos_dummy;
    }

    if (this->callbacks_.on_sample_rejected) {
        this->rti_sub_listener_->as_datareaderlistener.on_sample_rejected =
            SubscriberListenerImpl::on_sample_rejected;
    } else {
        this->rti_sub_listener_->as_datareaderlistener.on_sample_rejected =
            SubscriberListenerImpl::on_sample_rejected_dummy;
    }

    if (this->callbacks_.on_liveliness_changed) {
        this->rti_sub_listener_->as_datareaderlistener.on_liveliness_changed =
            SubscriberListenerImpl::on_liveliness_changed;
    } else {
        this->rti_sub_listener_->as_datareaderlistener.on_liveliness_changed =
            SubscriberListenerImpl::on_liveliness_changed_dummy;
    }

    if (this->callbacks_.on_data_available) {
        this->rti_sub_listener_->as_datareaderlistener.on_data_available =
            SubscriberListenerImpl::on_data_available;
    } else {
        this->rti_sub_listener_->as_datareaderlistener.on_data_available =
            SubscriberListenerImpl::on_data_available_dummy;
    }

    if (this->callbacks_.on_subscription_matched) {
        this->rti_sub_listener_->as_datareaderlistener.on_subscription_matched =
            SubscriberListenerImpl::on_subscription_matched;
    } else {
        this->rti_sub_listener_->as_datareaderlistener.on_subscription_matched =
            SubscriberListenerImpl::on_subscription_matched_dummy;
    }

    this->rti_sub_listener_->as_datareaderlistener.as_listener.listener_data =
        reinterpret_cast<void*>(&this->callbacks_);
}
SubscriberListenerImpl::~SubscriberListenerImpl()
{
    if (this->rti_sub_listener_) {
        delete this->rti_sub_listener_;
        this->rti_sub_listener_ = nullptr;
    }
}

DDS_SubscriberListener* SubscriberListenerImpl::getListener()
{
    return this->rti_sub_listener_;
}

void SubscriberListenerImpl::on_data_on_readers(void* listener_data, DDS_Subscriber* sub)
{
    dal_subscriber_listener_callbacks_t* callbacks =
        reinterpret_cast<dal_subscriber_listener_callbacks_t*>(listener_data);
    if (callbacks->on_data_on_readers == nullptr)
        return;

    dal_subscriber_t* dal_sub = EntityMappingGetSubscriber(sub);
    if (!dal_sub) {
        return;
    }

    void* data = callbacks->data_;
    (callbacks->on_data_on_readers)(dal_sub, data);
}

void SubscriberListenerImpl::on_sample_lost(void* listener_data,
                                            DDS_DataReader* reader,
                                            const DDS_SampleLostStatus* status)
{
    dal_subscriber_listener_callbacks_t* callbacks =
        reinterpret_cast<dal_subscriber_listener_callbacks_t*>(listener_data);
    if (callbacks->on_sample_lost == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    dal_sample_lost_status_t dal_status;
    StatusConverter::GetInstance()->ConvertSampleLostStatusFromRtiddsToDal(status, &dal_status);

    void* data = callbacks->data_;
    (callbacks->on_sample_lost)(dal_reader, &dal_status, data);
}

void SubscriberListenerImpl::on_data_available(void* listener_data, DDS_DataReader* reader)
{
    dal_subscriber_listener_callbacks_t* callbacks =
        reinterpret_cast<dal_subscriber_listener_callbacks_t*>(listener_data);
    if (callbacks->on_data_available == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    void* data = callbacks->data_;
    (callbacks->on_data_available)(dal_reader, data);
}

void SubscriberListenerImpl::on_sample_rejected(void* listener_data,
                                                DDS_DataReader* reader,
                                                const DDS_SampleRejectedStatus* status)
{
    dal_subscriber_listener_callbacks_t* callbacks =
        reinterpret_cast<dal_subscriber_listener_callbacks_t*>(listener_data);
    if (callbacks->on_sample_rejected == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    dal_sample_rejected_status_t dal_status;
    StatusConverter::GetInstance()->ConvertSampleRejectedStatusFromRtiddsToDal(status, &dal_status);

    void* data = callbacks->data_;
    (callbacks->on_sample_rejected)(dal_reader, &dal_status, data);
}

void SubscriberListenerImpl::on_liveliness_changed(void* listener_data,
                                                   DDS_DataReader* reader,
                                                   const DDS_LivelinessChangedStatus* status)
{
    dal_subscriber_listener_callbacks_t* callbacks =
        reinterpret_cast<dal_subscriber_listener_callbacks_t*>(listener_data);
    if (callbacks->on_liveliness_changed == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    dal_liveliness_changed_status_t dal_status;
    StatusConverter::GetInstance()->ConvertLivelinessChangedStatusFromRtiddsToDal(status,
                                                                                  &dal_status);

    void* data = callbacks->data_;
    (callbacks->on_liveliness_changed)(dal_reader, &dal_status, data);
}

void SubscriberListenerImpl::on_requested_deadline_missed(
    void* listener_data, DDS_DataReader* reader, const DDS_RequestedDeadlineMissedStatus* status)
{
    dal_subscriber_listener_callbacks_t* callbacks =
        reinterpret_cast<dal_subscriber_listener_callbacks_t*>(listener_data);
    if (callbacks->on_requested_deadline_missed == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    dal_requested_deadline_missed_status_t dal_status;
    StatusConverter::GetInstance()->ConvertRequestedDeadlineMissedStatusFromRtiddsToDal(
        status, &dal_status);

    void* data = callbacks->data_;
    (callbacks->on_requested_deadline_missed)(dal_reader, &dal_status, data);
}

void SubscriberListenerImpl::on_requested_incompatible_qos(
    void* listener_data, DDS_DataReader* reader, const DDS_RequestedIncompatibleQosStatus* status)
{
    dal_subscriber_listener_callbacks_t* callbacks =
        reinterpret_cast<dal_subscriber_listener_callbacks_t*>(listener_data);
    if (callbacks->on_requested_incompatible_qos == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    dal_requested_incompatible_qos_status_t dal_status;
    StatusConverter::GetInstance()->ConvertRequestedIncompatibleQosStatusFromRtiddsToDal(
        status, &dal_status);

    void* data = callbacks->data_;
    (callbacks->on_requested_incompatible_qos)(dal_reader, &dal_status, data);
}

void SubscriberListenerImpl::on_subscription_matched(void* listener_data,
                                                     DDS_DataReader* reader,
                                                     const DDS_SubscriptionMatchedStatus* status)
{
    dal_subscriber_listener_callbacks_t* callbacks =
        reinterpret_cast<dal_subscriber_listener_callbacks_t*>(listener_data);
    if (callbacks->on_subscription_matched == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    dal_subscription_matched_status_t dal_status;
    StatusConverter::GetInstance()->ConvertSubscriptionMatchedStatusFromRtiddsToDal(status,
                                                                                    &dal_status);

    void* data = callbacks->data_;
    (callbacks->on_subscription_matched)(dal_reader, &dal_status, data);
}

void SubscriberListenerImpl::on_data_on_readers_dummy(void* listener_data, DDS_Subscriber* sub) {}
void SubscriberListenerImpl::on_requested_deadline_missed_dummy(
    void* listener_data, DDS_DataReader* reader, const DDS_RequestedDeadlineMissedStatus* status)
{
}
void SubscriberListenerImpl::on_requested_incompatible_qos_dummy(
    void* listener_data, DDS_DataReader* reader, const DDS_RequestedIncompatibleQosStatus* status)
{
}
void SubscriberListenerImpl::on_sample_rejected_dummy(void* listener_data,
                                                      DDS_DataReader* reader,
                                                      const DDS_SampleRejectedStatus* status)
{
}
void SubscriberListenerImpl::on_liveliness_changed_dummy(void* listener_data,
                                                         DDS_DataReader* reader,
                                                         const DDS_LivelinessChangedStatus* status)
{
}
void SubscriberListenerImpl::on_data_available_dummy(void* listener_data, DDS_DataReader* reader) {}
void SubscriberListenerImpl::on_subscription_matched_dummy(
    void* listener_data, DDS_DataReader* reader, const DDS_SubscriptionMatchedStatus* status)
{
}
void SubscriberListenerImpl::on_sample_lost_dummy(void* listener_data,
                                                  DDS_DataReader* reader,
                                                  const DDS_SampleLostStatus* status)
{
}
